var app = getApp();

Component({

  data: {
    isLogin: false
  },

  ready: function() {
    console.log(this.data.isLogin)
  },

  attached: function () {
    this.setData({
      isLogin: app.globalData.isLogin
    })
  },

  methods: {

    jtlogin: function () {
      
    },

    _getUserInfo: function(a) {
     
      app.globalData.isLogin = true;
      var that = this;
      that.setData({
        isLogin: true
      }), 
      wx.showLoading({
        title: "登录中..."
      }), 
      // 登录
      wx.login({
        success: function (res) {
          if (res.code) {

            console.log("code====" + res.code)
            //发起网络请求
            wx.request({
              url: app.globalData.url + 'Login',
              header: {
                'content-type': 'application/x-www-form-urlencoded' // 默认值
              },
              data: {
                code: res.code
              },
              method: 'GET',
              success: function (res) {
                console.log("login")
                console.log(res.data)
                
                var myUserInfo = res.data.ResultObject;
                
                if (!myUserInfo.IsRegister) {

                  // 获取用户信息
                  wx.getSetting({
                    success: res => {
                      if (res.authSetting['scope.userInfo']) {
                        // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                        wx.getUserInfo({
                          success: res => {
                            // 可以将 res 发送给后台解码出 unionId
                            app.globalData.wxuserInfo = res.userInfo
                            console.log(myUserInfo.Unionid)
                            wx.request({
                              url: app.globalData.url + 'Register',
                              header: {
                                'content-type': 'application/x-www-form-urlencoded' //默认值
                              },
                              data: {
                                "Unionid": myUserInfo.Unionid,
                                "openid": myUserInfo.OpenId,
                                "name": res.userInfo.nickName,
                                "sex": res.userInfo.gender,
                                "avatarurl": res.userInfo.avatarUrl
                              },
                              success: function (res) {
                                var _userInfo = res.data.ResultObject;
                                console.log(_userInfo);
                                app.globalData.userInfo = _userInfo;

                                that.triggerEvent('login', null, null);

                                wx.hideLoading(),
                                console.log(res.data),
                                that.setData({
                                    isLogin: true
                                  })
                              }, 
                              fail: function () {
                                wx.hideLoading(), 
                                that.setData({
                                  isLogin: true
                                });
                              }
                              
                            })
                          },
                          fail: function () {
                            wx.hideLoading(),
                              that.setData({
                                isLogin: true
                              });
                          }
                        })
                      }
                    },
                    fail: function () {
                      wx.hideLoading(),
                        that.setData({
                          isLogin: true
                        });
                    }
                  })
                } else {

          

                  app.globalData.userInfo = myUserInfo;
                  wx.getUserInfo({
                    success: res => {
                      app.globalData.wxuserInfo = res.userInfo
                    }
                  })


                  that.triggerEvent('login', null, null);
                  wx.hideLoading(),
                    that.setData({
                      isLogin: true
                    });
                }
              },
              fail: function () {
                wx.hideLoading(),
                  that.setData({
                    isLogin: true
                  });
              }
            })
          } else {
            console.log('登录失败！' + res.errMsg)
            wx.hideLoading(),
              that.setData({
                isLogin: true
              });
          }
        }
      })
    }
  }
});